package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			System.out.println("Please specify parameters: <amount of test data> <file> <format>");
			return;
		}
		
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if (file.exists()) {
			System.out.println("File exists, please remove it manually: " + file);
			return;
		}		
		
		List<ContactData> contacts = generateRandomContacts(amount);
		if ("csv".equals(format)) {
			saveContactsToCsvFile(contacts, file);
			}  	else if  ("xml".equals(format)) {
				saveContactsToXmlFile(contacts, file);
			}	else {
				System.out.println("Unknown format " + format);
				return;
			}
	}

	private static void saveContactsToXmlFile(List<ContactData> contacts, File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer= new FileWriter(file);
		writer.write(xml);
		writer.close();
	}

	public static List<ContactData> loadContactsFromXmlFile(File file) {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}
	
	private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException {
		FileWriter writer= new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.getFirstname() 	+ "," + contact.getLastname() + 
					"," + contact.getAddress() 		+ "," + contact.getHomePhone() + 
					"," + contact.getMobilePhone() 	+ "," + contact.getWorkPhone() + 
					"," + contact.getEmail() 		+ "," + contact.getEmail2() + 
					"," + contact.getBday() 		+ "," + contact.getBmonth() + 
					"," + contact.getByear() 		+ "," + contact.getAddress2() + 
					"," + contact.getPhone2() 		+ "," + contact.getGroup() + 
					",!" +
					"," + "\n");
		}
		writer.close();
	}

	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		List<ContactData> list = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null) {
			String[] part = line.split(",");
			ContactData contact = new ContactData()
				.withFirstname(part[0])
				.withLastname(part[1])
				.withAddress(part[2])
				.withHomePhone(part[3])
				.withMobilePhone(part[4])
				.withWorkPhone(part[5])
				.withEmail(part[6])
				.withEmail2(part[7])
				.withBday(part[8])
				.withBmonth(part[9])
				.withByear(part[10])
				.withAddress2(part[11])
				.withPhone2(part[12])
				.withGroup(part[13]);
			list.add(contact);	
			line = bufferedReader.readLine();
		} 
		bufferedReader.close();
		return list;
	}
	
	public static List<ContactData> generateRandomContacts(int amount) {
		 List<ContactData> list = new ArrayList<ContactData>();
		 for (int i = 0; i < amount; i++) {
			  	ContactData contact = new ContactData()
			  	.withFirstname(generateRandomStrings())
				.withLastname(generateRandomStrings())
				.withAddress(generateRandomStrings())
				.withHomePhone(generateRandomStrings())
				.withMobilePhone(generateRandomStrings())
				.withWorkPhone(generateRandomStrings())
				.withEmail(generateRandomStrings())
				.withEmail2(generateRandomStrings())
				.withBday(generateRandomDay())
				.withBmonth(generateRandomMonth())
				.withByear(generateRandomYear())
				.withAddress2(generateRandomStrings())
				.withPhone2(generateRandomStrings())
				.withGroup(generateRandomGroup());
			 list.add(contact);
		 }
		 return list;
	}
	
	private static String generateRandomGroup() {
		return "-";
	}

	public static String generateRandomStrings() {
 		Random rnd = new Random();		 
 		if (rnd.nextInt(4) == 0) {
 				 return "contact";
 				 } else {
 					 return "book" + rnd.nextInt();
 				 }
 	}

	public static String generateRandomDay() {
		Random rnd = new Random();
		int index1 = rnd.nextInt(31);
		if (index1 == 0) 
			{return "-";
			} else {
				return String.valueOf(index1);
					}	
	}

	private static String generateRandomMonth() {
		Random rnd = new Random();
		int index2 = rnd.nextInt(12);
		if (index2 == 1) {return "January";}
			else if (index2 == 2) {return "February";}
				else if (index2 == 3) {return "March";}
					else if (index2 == 4) {return "April";}
						else if (index2 == 5) {return "May";}
							else if (index2 == 6) {return "June";}
								else if (index2 == 7) {return "July";}
									else if (index2 == 8) {return "August";}
										else if (index2 == 9) {return "September";}
											else if (index2 == 10) {return "October";}
												else if (index2 == 11) {return "November";}
													else if (index2 == 12) {return "December";}
		else {
			return "-";
			}
	}
	
	private static String generateRandomYear() {
		Random rnd = new Random();
		int index3 = rnd.nextInt(99);
		return "19" + String.valueOf(index3);
	}
}


